package org.rsbot.script.methods;

import java.util.ArrayList;
import java.util.List;
import org.rsbot.client.Client;
import org.rsbot.client.HashTable;
import org.rsbot.client.NodeListCache;
import org.rsbot.script.internal.wrappers.Deque;
import org.rsbot.script.util.Filter;
import org.rsbot.script.wrappers.RSGroundItem;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;

public class GroundItems extends MethodProvider
{
  public static final Filter<RSGroundItem> ALL_FILTER = new Filter() {
    public boolean accept(RSGroundItem paramRSGroundItem) {
      return true;
    }
  };

  GroundItems(MethodContext paramMethodContext)
  {
    super(paramMethodContext);
  }

  public RSGroundItem[] getAll()
  {
    return getAll(52, ALL_FILTER);
  }

  public RSGroundItem[] getAll(Filter<RSGroundItem> paramFilter)
  {
    return getAll(52, paramFilter);
  }

  public RSGroundItem[] getAll(int paramInt)
  {
    return getAll(paramInt, ALL_FILTER);
  }

  public RSGroundItem[] getAll(int paramInt, Filter<RSGroundItem> paramFilter)
  {
    ArrayList localArrayList = new ArrayList();
    int i = this.methods.players.getMyPlayer().getLocation().getX();
    int j = this.methods.players.getMyPlayer().getLocation().getY();
    int k = i - paramInt; int m = j - paramInt;
    int n = i + paramInt; int i1 = j + paramInt;
    for (int i2 = k; i2 < n; i2++) {
      for (int i3 = m; i3 < i1; i3++) {
        RSGroundItem[] arrayOfRSGroundItem1 = getAllAt(i2, i3);
        for (RSGroundItem localRSGroundItem : arrayOfRSGroundItem1) {
          if (paramFilter.accept(localRSGroundItem)) {
            localArrayList.add(localRSGroundItem);
          }
        }
      }
    }
    return (RSGroundItem[])localArrayList.toArray(new RSGroundItem[localArrayList.size()]);
  }

  public RSGroundItem getNearest(Filter<RSGroundItem> paramFilter)
  {
    int i = 9999999;
    int j = this.methods.players.getMyPlayer().getLocation().getX();
    int k = this.methods.players.getMyPlayer().getLocation().getY();
    int m = j - 52; int n = k - 52;
    int i1 = j + 52; int i2 = k + 52;
    Object localObject = null;
    for (int i3 = m; i3 <= i1; i3++) {
      for (int i4 = n; i4 <= i2; i4++) {
        RSGroundItem[] arrayOfRSGroundItem1 = getAllAt(i3, i4);
        for (RSGroundItem localRSGroundItem : arrayOfRSGroundItem1) {
          if ((paramFilter.accept(localRSGroundItem)) && (this.methods.calc.distanceTo(localRSGroundItem.getLocation()) < i)) {
            i = this.methods.calc.distanceTo(localRSGroundItem.getLocation());
            localObject = localRSGroundItem;
          }
        }
      }
    }
    return localObject;
  }

  public RSGroundItem getNearest(int[] paramArrayOfInt)
  {
    return getNearest(new Filter(paramArrayOfInt) {
      public boolean accept(RSGroundItem paramRSGroundItem) {
        int i = paramRSGroundItem.getItem().getID();
        for (int m : this.val$ids) {
          if (m == i) {
            return true;
          }
        }
        return false;
      }
    });
  }

  public RSGroundItem[] getAllAt(int paramInt1, int paramInt2)
  {
    if (!this.methods.game.isLoggedIn()) {
      return new RSGroundItem[0];
    }
    ArrayList localArrayList = new ArrayList();

    HashTable localHashTable = this.methods.client.getRSItemHashTable();
    int i = paramInt1 | paramInt2 << 14 | this.methods.client.getPlane() << 28;

    NodeListCache localNodeListCache = (NodeListCache)this.methods.nodes.lookup(localHashTable, i);

    if (localNodeListCache == null) {
      return new RSGroundItem[0];
    }
    Deque localDeque = new Deque(localNodeListCache.getNodeList());
    for (org.rsbot.client.RSItem localRSItem = (org.rsbot.client.RSItem)localDeque.getHead(); localRSItem != null; localRSItem = (org.rsbot.client.RSItem)localDeque.getNext()) {
      localArrayList.add(new RSGroundItem(this.methods, new RSTile(paramInt1, paramInt2), new org.rsbot.script.wrappers.RSItem(this.methods, localRSItem)));
    }

    return (RSGroundItem[])localArrayList.toArray(new RSGroundItem[localArrayList.size()]);
  }

  public RSGroundItem[] getAllAt(RSTile paramRSTile)
  {
    return getAllAt(paramRSTile.getX(), paramRSTile.getY());
  }
}